package com.aliyun.heiyu.powermaster.server.dao.impl;

import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.powermaster.server.dao.DeviceSpecDao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @Description 工控机实现类
 * @Author shun.zhang
 * @Date 2020/11/23 16:59
 **/
public class DeviceSpecIpcImpl implements DeviceSpecDao {
    @Override
    public List<Map<String, String>> findAll() {
        String sql = "SELECT device_id,i_port,agreement,serial_port,baud_rate,parity_bit,data_bit,stop_bit,weight from d_ipc where status = 1";
        return JdbcUtil.queryForList_Map(sql,null);
    }

    @Override
    public Map<String, String> findOne(String deviceId) {
        String sql = "SELECT device_id,i_port,agreement,serial_port,baud_rate,parity_bit,data_bit,stop_bit,weight from d_ipc where device_id = ?";
        Object[] obj = new Object[1];
        obj[0] = deviceId;
        return JdbcUtil.queryForMap(sql,obj);
    }

    @Override
    public boolean insert(Map<String, String> map) throws SQLException {
        String sql = "REPLACE INTO d_ipc(device_id,i_port,agreement,status,serial_port,baud_rate,parity_bit,data_bit,stop_bit,weight) VALUES(?,?,?,?,?,?,?,?,?,?)";
        Object[] obj = new Object[10];
        obj[0] = map.get("device_id");
        obj[1] = Integer.parseInt(map.get("i_port"));
        obj[2] = map.get("agreement");
        obj[3] = 1;
        obj[4] = map.get("serial_port");
        obj[5] = map.get("baud_rate");
        obj[6] = map.get("parity_bit");
        obj[7] = map.get("data_bit");
        obj[8] = map.get("stop_bit");
        obj[9] = map.get("weight");
        return JdbcUtil.update(sql, obj);
    }

    @Override
    public boolean update(Map<String, String> map) throws SQLException {
        String sql = "update d_ipc set agreement=?,serial_port=?,baud_rate=?,parity_bit=?,data_bit=?,stop_bit=? where device_id = ?";
        Object[] obj = new Object[7];
        obj[0] = map.get("agreement");
        obj[1] = map.get("serial_port");
        obj[2] = map.get("baud_rate");
        obj[3] = map.get("parity_bit");
        obj[4] = map.get("data_bit");
        obj[5] = map.get("stop_bit");
        obj[6] = map.get("device_id");
        return JdbcUtil.update(sql,obj);
    }

    @Override
    public boolean delete(String deviceId) throws SQLException {
        String sql = "update d_ipc set status = 0 where device_id = ?";
        Object[] obj = new Object[1];
        obj[0] = deviceId;
        return JdbcUtil.update(sql,obj);
    }

    @Override
    public boolean delete(String[] sns) throws SQLException {
        StringBuffer sqlBuffer = new StringBuffer();
        int length = sns.length;
        sqlBuffer.append("update d_ipc set status = 0 WHERE device_id IN ");
        sqlBuffer.append("(");
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                sqlBuffer.append("?)");
            } else {
                sqlBuffer.append("?,");
            }
        }
        String sql = sqlBuffer.toString();
        return JdbcUtil.update1(sql, sns);
    }
}
